//
//  LWHomeCellModel.swift
//  Microblog
//
//  Created by Roman on 17/1/9.
//  Copyright © 2017年 Roman. All rights reserved.
//

import UIKit

class LWHomeCellModel: NSObject {
    
    
    //用户等级
    var mbrankImage: UIImage?
    
    //和认证信息
    var verifiedImage: UIImage?
    
    //微博来源
    var ScurceText: String?
    //微博时间
    var timeText: String? {
        // Thu Jan 12 22:15:07 +0800 2017
      //  print(str)
        //1.实例化日期对象
        let str = statuses?.created_at ?? ""
        let dateFormatter = DateFormatter()
        //2.根据日期字符串的格式设置格式化符号
        dateFormatter.dateFormat = "EEE MMM dd HH:mm:ss Z yyyy"
        //2.1 一定要注意需要设置本地化信息,否者真机上无法调试
        dateFormatter.locale = Locale(identifier: "haha")
        
        //3.根据格式化字符将字符串转换成日期对象
        guard let sinaDate = dateFormatter.date(from: str) else{
            print("穿越时空发布的微博")
            return ""
        }
        //print(sinaDate)
        
        //判断是否是今年
        let calendar = Calendar.current
        //获取当前的日期
        let currentDate = Date()
        //通过比较当前日期和微博发布日期判断是否是今年
        let result = calendar.dateComponents([.year], from: sinaDate, to: currentDate)
        
        if result.year == 0 {
            //今年
            if calendar.isDateInToday(sinaDate) {
                //今天
                //获取sinaDate和当前日期隔了多少秒
                let delta = currentDate.timeIntervalSince(sinaDate)
                if delta < 60 {
                    return "刚刚"
                }else if delta > 60 && delta < 3600{
                    return "\(Int(delta/60))分钟前"
                }else{
                    return "\(Int(delta / 3600))小时前"
                }
            }else{
                //非今天,昨天,和早于昨天
                if calendar.isDateInYesterday(sinaDate){
                    //昨天
                    dateFormatter.dateFormat = "昨天 HH:mm"
                    return dateFormatter.string(from: sinaDate)
                }else{
                    //早昨天的 ,MM dd
                    dateFormatter.dateFormat = "MM-dd"
                    return dateFormatter.string(from: sinaDate)
                }
            }
        }else{
            //非今年
            dateFormatter.dateFormat = "yyyy-MM-dd"
            return dateFormatter.string(from: sinaDate)
        }
        
        // return ""
    }
    //根据返回原创还是转发,来选择不同的视图模型数组
    var imageType: [LWPicturModel]? {
        if statuses?.retweeted_status == nil {
            //原创
            return statuses?.pic_urls
            
        }
        return statuses?.retweeted_status?.pic_urls
        
    } 
    
    
    var headURL: URL?
    var statuses: LWstatusesModel? {
        didSet{
            //处理数据
            //头像
            headURL = URL(string: statuses?.user?.avatar_large ?? "")
            
            //给对象信息赋值
            dealmbrankImage()
            dealverifiedImage()
            
            //self.timeText = dealWeiboTime(str: statuses?.created_at ?? "")
            self.ScurceText = dealWeibiSource(str: statuses?.source ?? "")
        }
    }
    /*
    /// 处理微博日期
    ///
    /// - Parameter str: 日期字符串
    /// - Returns: 返回描述字符串
    private func dealWeiboTime(str: String ) -> String {
        // Thu Jan 12 22:15:07 +0800 2017
        print(str)
        //1.实例化日期对象
        let dateFormatter = DateFormatter()
        //2.根据日期字符串的格式设置格式化符号
        dateFormatter.dateFormat = "EEE MMM dd HH:mm:ss Z yyyy"
        //2.1 一定要注意需要设置本地化信息,否者真机上无法调试
        dateFormatter.locale = Locale(identifier: "haha")
        
        //3.根据格式化字符将字符串转换成日期对象
        guard let sinaDate = dateFormatter.date(from: str) else{
            print("穿越时空发布的微博")
            return ""
        }
        //print(sinaDate)
        
        //判断是否是今年
        let calendar = Calendar.current
        //获取当前的日期
        let currentDate = Date()
        //通过比较当前日期和微博发布日期判断是否是今年
       let result = calendar.dateComponents([.year], from: sinaDate, to: currentDate)
        
        if result.year == 0 {
            //今年
            if calendar.isDateInToday(sinaDate) {
                //今天
                //获取sinaDate和当前日期隔了多少秒
                let delta = currentDate.timeIntervalSince(sinaDate)
                if delta < 60 {
                    return "刚刚"
                }else if delta > 60 && delta < 3600{
                    return "\(Int(delta/60))分钟前"
                }else{
                    return "\(Int(delta / 3600))小时前"
                }
            }else{
                //非今天,昨天,和早于昨天
                if calendar.isDateInYesterday(sinaDate){
                    //昨天
                    dateFormatter.dateFormat = "昨天 HH:mm"
                    return dateFormatter.string(from: sinaDate)
                }else{
                    //早昨天的 ,MM dd
                    dateFormatter.dateFormat = "MM-dd"
                    return dateFormatter.string(from: sinaDate)
                }
            }
        }else{
            //非今年
            dateFormatter.dateFormat = "yyyy-MM-dd"
            return dateFormatter.string(from: sinaDate)
        }
        
       // return ""
        
    }
    */
    
    private func dealWeibiSource(str: String) -> String {
        //<a href="http://app.weibo.com/t/feed/6vtZb0" rel="nofollow">微博 weibo.com</a>
       // print(str)
        let str = "<a href=\"http://app.weibo.com/t/feed/6vtZb0\" rel=\"nofollow\">微博 weibo.com</a>"
        let startFlag = "\">"
        let endFlag = "</a>"
        
        guard let startRange = str.range(of: startFlag),
            let endRange = str.range(of: endFlag) else{
            return "来自火星"
        }
        //lowerBound 表示范围的最小边界
        //upperBound 表示范围的最大边界
        let startIndex = startRange.upperBound
        let endIndex = endRange.lowerBound
        let subStr = str.substring(with: startIndex..<endIndex)
        return subStr
        
    }
    
   
    //处理用户等级  //会员等级  /// 会员等级 1- 6
    private func dealmbrankImage() {
        let mbrank = statuses?.user?.mbrank ?? 0
        
        if mbrank > 0 && mbrank < 7{
            mbrankImage = UIImage(named: "common_icon_membership_level\(mbrank)")
        }else{
            mbrankImage = UIImage(named: "common_icon_membership_expired")
        }
        
    }
    //处理认证信息  //-1：没有认证，0：认证用户，2,3,5: 企业认证，220: 达人(草根)
    //用switch
    private func dealverifiedImage() {
        let verifiedType = statuses?.user?.verified_type ?? -1
        switch verifiedType {
        case 0:
            verifiedImage = #imageLiteral(resourceName: "avatar_vip")
        case 2,3,5:
            verifiedImage = #imageLiteral(resourceName: "avatar_enterprise_vip")
        case 220:
            verifiedImage = #imageLiteral(resourceName: "avatar_grassroot")
        default:
            print("没有认证")
        }
        
    }
    

}
